<script setup name="Person" lang="ts">
import {watch, reactive} from "vue";

// 数据
let person = reactive({
  name: "张三",
  age: 18,
  car: {
    c1: "奔驰",
    c2: "宝马"
  }
})

// 方法
function changeName() {
  person.name += "~"
}

function changeAge() {
  person.age += 1
}

function changeCarC1() {
  person.car.c1 = "奥迪"
}

function changeCarC2() {
  person.car.c2 = "大众"
}

function changeCar() {
  person.car = {
    c1: "比亚迪",
    c2: "特斯拉"
  }
}

// 监听
// // 监视姓名和人的第一台车
// watch([() => person.name, () => person.car.c1], (newValue, oldValue) => {
//   console.log("监视姓名和第一台车：", newValue, oldValue)
// })

// 监视姓名和所有车
watch([() => person.name, () => person.car], (newValue, oldValue) => {
  console.log("监视姓名和车：", newValue, oldValue)
})
</script>

<template>
  <div class="person">
    <h1>情况五：监视上述的多个数据</h1>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <h2>汽车：{{ person.car.c1 }}、{{ person.car.c2 }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="changeCarC1">修改第一台汽车</button>
    <button @click="changeCarC2">修改第二台汽车</button>
    <button @click="changeCar">修改整个汽车</button>


  </div>
</template>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px #000;
  border-radius: 10px;
  padding: 20px;
}

button {
  margin: 0 5px;
}

li {
  font-size: 20px;
}
</style>
